package com.hetaozi.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hetaozi.login.entity.*;
import com.hetaozi.login.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hetaozi.login.mapper.SysUserMapper;

/**
 * @email： wf520200@163.com
 * @date: 2021/11/11 10:43
 * 功能：
 * 描述：
 * @author： 吴帆
 **/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Override
    public int updateBatch(List<SysUser> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<SysUser> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<SysUser> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public int insertOrUpdate(SysUser record) {
        return baseMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(SysUser record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Override
    public SysUser getUserByUserName(String userName) {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("user_name",userName).eq("deleted",0);
        SysUser sysUser = baseMapper.selectOne(wrapper);

        if (sysUser !=null){
            Long id = sysUser.getId();
            List<SysUserRole> sysUserRoles=sysUserRoleService.getListRoleByUserId(id);

            List<Long> roleIds = sysUserRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
            if (roleIds.size()>0){

                List<SysRole> roleList = sysRoleService.listByIds(roleIds);
                List<String> roles = roleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
                sysUser.setRoles(new HashSet<>(roles));

                List<SysRolePermission> sysRolePermissions = sysRolePermissionService.getSysRolePermissionByRoleIds(roleIds);
                List<Long> permissionsIds = sysRolePermissions.stream().map(SysRolePermission::getPermissionId).collect(Collectors.toList());

                if (permissionsIds.size()>0){
                    List<SysPermission> permissionsList = sysPermissionService.listByIds(permissionsIds);
                    List<String> permissions = permissionsList.stream().map(SysPermission::getPermissionName).collect(Collectors.toList());
                    sysUser.setPermissions(new HashSet<>(permissions));
                }

            }

        }
        return sysUser;
    }
}

